package com.sven.sharedb.range.service.impl;

import com.sven.sharedb.core.utils.DateTimeUtils;
import com.sven.sharedb.range.entity.ShardingOrderWz;
import com.sven.sharedb.range.entity.dto.UpdateByExampleDTO;
import com.sven.sharedb.range.mapper.ShardingOrderWzMapper;
import com.sven.sharedb.range.service.IShareDbService;
import com.sven.sharedb.core.utils.GsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author sven.wang
 * @version 2022/4/26 17:58
 */
@Slf4j
@Service
public class ShareDbServiceImpl implements IShareDbService {

    @Resource
    private ShardingOrderWzMapper shardingOrderWzMapper;

    @Override
    public String selectBySql(String orderId, Date createDate, Date beginDate, Date endDate) {
        List<ShardingOrderWz> shardingOrderWz = shardingOrderWzMapper.selectBySql(orderId, createDate, beginDate, endDate);
        return GsonUtils.toJson(shardingOrderWz);
    }

    @Override
    public String selectByExample(String orderId, Date createDate, Date beginDate, Date endDate) {
        Example example = new Example(ShardingOrderWz.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(orderId)) {
            criteria.andEqualTo("orderId", orderId);
        }
        if (createDate != null) {
            criteria.andEqualTo("createDate", createDate);
        }
        if (beginDate != null && endDate != null) {
            criteria.andBetween("createDate", beginDate, endDate);
        }
        List<ShardingOrderWz> shardingOrderWz = shardingOrderWzMapper.selectByExample(example);
        return GsonUtils.toJson(shardingOrderWz);
    }

    @Override
    public int updateBySql(UpdateByExampleDTO dto) {
        int updateResult =  shardingOrderWzMapper.updateBySql(dto);
        return updateResult;
    }

    @Override
    public int updateByExample(UpdateByExampleDTO dto) {
        Example example = new Example(ShardingOrderWz.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(dto.getOrderId())) {
            criteria.andEqualTo("orderId", dto.getOrderId());
        }
        if (dto.getCreateDate() != null) {
            criteria.andEqualTo("createDate", dto.getCreateDate());
        }
        if (dto.getBeginDate() != null && dto.getEndDate() != null) {
            criteria.andBetween("createDate", dto.getBeginDate(), dto.getEndDate());
        }

        ShardingOrderWz update = new ShardingOrderWz();
        update.setName(dto.getUpdateName());
        int updateResult = shardingOrderWzMapper.updateByExampleSelective(update, example);
        return updateResult;
    }

    @Override
    public int deleteBySql(UpdateByExampleDTO dto) {
        int updateResult =  shardingOrderWzMapper.deleteBySql(dto);
        return updateResult;
    }

    @Override
    public int deleteByExample(UpdateByExampleDTO dto) {
        Example example = new Example(ShardingOrderWz.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(dto.getOrderId())) {
            criteria.andEqualTo("orderId", dto.getOrderId());
        }
        if (dto.getCreateDate() != null) {
            criteria.andEqualTo("createDate", dto.getCreateDate());
        }
        if (dto.getBeginDate() != null && dto.getEndDate() != null) {
            criteria.andBetween("createDate", dto.getBeginDate(), dto.getEndDate());
        }
        int updateResult = shardingOrderWzMapper.deleteByExample(example);
        return updateResult;
    }

    @Override
    public String selectJoinDetailBySql(String orderId, Date createDate, Date beginDate, Date endDate) {
        List<ShardingOrderWz> shardingOrderWz = shardingOrderWzMapper.selectJoinDetailBySql(orderId, createDate, beginDate, endDate);
        return GsonUtils.toJson(shardingOrderWz);
    }
}
